<?php

namespace frontend\modules\resource\controllers;

use frontend\modules\common\controllers\BaseController;
use frontend\modules\common\models\BaseParam;
use frontend\modules\resource\logic\AdvancedSearchLogic;
use frontend\modules\resource\logic\ResourceImportLogic;
use frontend\modules\resource\logic\ResourceLogic;
use OSS\Core\OssException;
use OSS\Http\RequestCore;
use OSS\Http\ResponseCore;
use OSS\OssClient;
use Yii;

/**
 * ResourceController implements the CRUD actions for HyBook model.
 */
class ResourceController extends BaseController
{
    public function init()
    {
        parent::init(); // TODO: Change the autogenerated stub
        $this->checkType();
    }

    /**
     * @return string
     * @throws \yii\base\ExitException
     */
    public function actionIndex()
    {

        $searchModel = (new ResourceLogic())->getResourceModelSearch($this->type);
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            'type' => $this->type,
        ]);


    }

    /**
     * @return string|\yii\web\Response
     * @throws \yii\base\ExitException
     * @throws \yii\web\NotFoundHttpException
     */
    public function actionCreate()
    {
        $logic = (new ResourceLogic());
        $model = $logic->getResourceModel($this->type);

        if (Yii::$app->request->post()) {

            $res = $logic->createResource($model, $this->type);
            if ($res) {
                return $this->redirect(['finish']);
            }
        }

        return $this->render('create', [
            'model' => $model,
            'type' => $this->type,
        ]);
    }

    /**
     * @param $id
     * @return string|\yii\web\Response
     * @throws \yii\base\ExitException
     */
    public function actionUpdate($id)
    {
        $model = (new ResourceLogic())->getResourceModel($this->type, $id);


        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['finish']);
        }

        return $this->render('update', [
            'model' => $model,
            'type' => $this->type,
        ]);
    }

    /**
     * @return string
     * @throws \yii\base\ExitException
     * @throws \yii\db\Exception
     */
    public function actionBatchDelete()
    {

        $id = Yii::$app->request->post('id');
        $r_type = Yii::$app->request->post('r_type');
        $id = json_decode($id, true);
        $res = (new AdvancedSearchLogic())->batchDelete($id, $r_type);
        return $this->suc(['data' => $res, '删除成功']);
    }

    /**
     * 分类接口
     * @return string
     */
    public function actionOption()
    {
        $cateId = Yii::$app->request->get('id', 'empty');
        $data = ResourceLogic::classesMap($cateId);
        $tmp = '';
        foreach ($data as $key => $val) {
            $tmp .= "<option value='" . $key . "'>{$val}</option>";
        }
        return $this->suc(['data' => $tmp]);
    }

    /**
     * @return string
     * @throws \PHPExcel_Exception
     * @throws \PHPExcel_Reader_Exception
     * @throws \yii\base\ExitException
     * @throws \yii\db\Exception
     * @throws \yii\web\NotFoundHttpException
     */
    public function actionImportExcel()
    {
        $type = Yii::$app->request->post('r_type');
        $file = Yii::$app->request->post('file');

        $num = (new ResourceLogic())->ImportData(
            BaseParam::$excelSavePath . $file,
            $type
        );
        return $this->suc(['msg' => '导入记录：' . $num . ' 条']);
    }


    /**
     * @throws \PHPExcel_Exception
     * @throws \PHPExcel_Reader_Exception
     * @throws \yii\base\ExitException
     * @throws \yii\db\Exception
     * @throws \yii\web\NotFoundHttpException
     */
    public function actionTest()
    {


        $accessKeyId = 'voReRBgHCHRM77Yq';         //获取阿里云oss的accessKeyId
        $accessKeySecret = 'NskojIP12N2edavVuta9Nec0ndBrb4'; //获取阿里云oss的accessKeySecret
        $endpoint = 'oss-cn-beijing.aliyuncs.com';               //获取阿里云oss的endPoint
        $bucket = 'hangyefenguan';

//        try {
//            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
//            $res = $ossClient->createBucket($bucket);
//            var_dump($res);
//        } catch (OssException $e) {
//            var_dump($e->getMessage());
//        }
        $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
        $timeout = 3600000;
        //$object = 'test1';
        $object = '111.docx';

        $filePath = '../runtime/excel/111.docx';
        //$signedUrl = $ossClient->signUrl($bucket, $object, $timeout);
        $res = $ossClient->uploadFile($bucket, $object, $filePath);
        var_dump('<br/>');
        var_dump($res);die;

    }

    public function actionTest2(){
        $accessKeyId = 'voReRBgHCHRM77Yq';         //获取阿里云oss的accessKeyId
        $accessKeySecret = 'NskojIP12N2edavVuta9Nec0ndBrb4'; //获取阿里云oss的accessKeySecret
        $endpoint = 'oss-cn-beijing.aliyuncs.com';               //获取阿里云oss的endPoint
        $bucket = 'hangyefenguan';

        $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
        $timeout = 3600000;
        $object = 'test1';


        $signedUrl = $ossClient->signUrl($bucket, $object, $timeout);

        var_dump($signedUrl);
    }


}
